//
//  JAddShopCarTopView.m
//  HeadIndustry
//
//  Created by jun on 2017/8/31.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JAddShopCarTopView.h"
#import "JShopCarSelBtnView.h"

@interface JAddShopCarTopView()

@property (nonatomic , strong) JShopCarSelBtnView *midView;
@property (nonatomic , strong) JShopCarSelBtnView *subView;
@property (nonatomic , strong) NSMutableDictionary *attributeDic;

@end

@implementation JAddShopCarTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _imgArr = [NSArray array];
        _btnSelArr = [NSMutableArray array];
        _btnImgArr = [NSMutableArray array];

        _attributeDic = [NSMutableDictionary dictionary];
        
        self.backgroundColor = [UIColor whiteColor];
        self.opaque = NO;
        [self setUpView];
        [self setselBtnView];
    }
    return  self;
}

- (void)setUpView {
    _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, 85 + 20)];
    _topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_topView];
    
    _productBtnImg = [UIButton buttonWithType:UIButtonTypeCustom];
    _productBtnImg.left = 10;
    _productBtnImg.top = 10;
    _productBtnImg.size = CGSizeMake(85, 85);
    [_productBtnImg addTarget:self action:@selector(productBtnImgClick:) forControlEvents:UIControlEventTouchUpInside];

    [_topView addSubview:_productBtnImg];
    
    _productName = [YYLabel new];
    _productName.left = _productBtnImg.right + 5;
    _productName.top = 10;
    _productName.size = CGSizeMake(kWindowW - 40 - 107, 40);
    _productName.font = kFONT_TITLE(16);
    _productName.numberOfLines = 0;
    _productName.textColor = [UIColor blackColor];
    [_topView addSubview:_productName];
    
    _concelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _concelBtn.right = kWindowW - 40;
    _concelBtn.top = 10;
    _concelBtn.size = CGSizeMake(40, 40);
    [_concelBtn addTarget:self action:@selector(cencleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_concelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [_topView addSubview:_concelBtn];
    
    _productPrice = [YYLabel new];
    _productPrice.top = _productName.bottom + 5;
    _productPrice.left = _productBtnImg.right +5;
    _productPrice.size = CGSizeMake(100, 20);
    _productPrice.textColor = [UIColor redColor];
    _productPrice.font = kFONT_TITLE(16);
    [_topView addSubview:_productPrice];
    
    _selSizeLa = [YYLabel new];
    _selSizeLa.top = _productPrice.bottom + 5;
    _selSizeLa.left = _productBtnImg.right +5;
    _selSizeLa.size = CGSizeMake(kWindowW - 85 - 20 - 10, 20);
    _selSizeLa.textColor = kRGBColor(150, 150, 150);
    _selSizeLa.font = kFONT_TITLE(14);
    [_topView addSubview:_selSizeLa];
    
    CALayer *line = [CALayer layer];
    line.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line.frame = CGRectMake(0, _topView.height - 1, kWindowW, 1);
    [_topView.layer addSublayer:line];
}

- (void)setselBtnView;
{
    _btnSelView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_topView.frame), kWindowW, 0)];
    _btnSelView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_btnSelView];
    
}

- (void)BtnSelClick:(NSInteger)num sel:(NSInteger)sel
{
    JProductAttributeModel *model = _dataArr[sel];
    NSString *str = model.attribute_values[num][@"PictureUrl"];
    if (str.length > 0) {
        UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]];
        [_productBtnImg setImage:img forState:UIControlStateNormal];
    }
    
    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:str];
    _imgArr = arr;
    _productPrice.text = [NSString stringWithFormat:@"¥ %.2f",(_productModel.Price + [model.attribute_values[num][@"price_adjustment"] floatValue])];
    
    if (sel == 0) {
        if (_attributeDic[@"model.product_attribute_name"]) {
            [_attributeDic removeObjectForKey:@"model.product_attribute_name"];
            [_attributeDic setValue:model.product_attribute_name forKey:@"model.product_attribute_name"];
        }
        else
        {
            [_attributeDic setValue:model.product_attribute_name forKey:@"model.product_attribute_name"];
        }
        
        if (_attributeDic[@"name"]) {
            [_attributeDic removeObjectForKey:@"name"];
            [_attributeDic setValue:model.attribute_values[num][@"name"] forKey:@"name"];
            
        }
        else
        {
            [_attributeDic setValue:model.attribute_values[num][@"name"] forKey:@"name"];
        }
    }
    else
    {
        if (_attributeDic[@"model.product_attribute_name2"]) {
            [_attributeDic removeObjectForKey:@"model.product_attribute_name2"];
            [_attributeDic setValue:model.product_attribute_name forKey:@"model.product_attribute_name2"];
        }
        else
        {
            [_attributeDic setValue:model.product_attribute_name forKey:@"model.product_attribute_name2"];
        }
        
        if (_attributeDic[@"name2"]) {
            [_attributeDic removeObjectForKey:@"name2"];
            [_attributeDic setValue:model.attribute_values[num][@"name"] forKey:@"name2"];
            
        }
        else
        {
            [_attributeDic setValue:model.attribute_values[num][@"name"] forKey:@"name2"];
        }
    }
    
    
    if ((_dataArr.count == 1 && _attributeDic.count == 2) || (_dataArr.count == 2 && _attributeDic.count == 4) ) {
        if (_productAttributeBlock) {
            _productAttributeBlock(_attributeDic);
        }
    }
    
}

- (void)setProductModel:(JProductDetailModel *)productModel
{
    _productModel = productModel;
    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_productModel.ImgUrl]]];
    [_productBtnImg setImage:img forState:UIControlStateNormal];
    
    _productName.text = _productModel.Name;
    _productPrice.text = _productModel.PriceText;
    
}

- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    if (dataArr.count > 0) {
        _selSizeLa.hidden = NO;
        
        if (dataArr.count == 1) {
            JProductAttributeModel *model = _dataArr[0];
            CGFloat height;
            if (model.product_attribute_id == 4) {
                
                CGRect rect = CGRectMake(0, 0, kWindowW, 100);
                [self addMidViewRect:rect dataArr:model.attribute_values];
                _midView.attributeModel = model;
                height = [_midView getViewHeight];
                _midView.frame = CGRectMake(0, 0, kWindowW, height);
            }
            else
            {
                CGRect rect = CGRectMake(0, 0, kWindowW, 165);
                [self addsubBtnViewRect:rect dataArr:model.attribute_values num:0];
                _subView.attributeModel = model;
                height = [_subView getViewHeight];
                _subView.frame = CGRectMake(0, 0, kWindowW, height);
            }
            
            _selSizeLa.text = [NSString stringWithFormat:@"请选择%@",model.product_attribute_name];
            _btnSelView.frame = CGRectMake(0, CGRectGetMaxY(_topView.frame), kWindowW, height);
        }
        else
        {
            
            NSMutableString *mStr = [NSMutableString stringWithFormat:@"请选择"];
            for (int i = 0; i < dataArr.count; i++) {
                   JProductAttributeModel *model = _dataArr[i];
                if (model.product_attribute_id == 4)
                {
                    CGRect rect = CGRectMake(0, 0, kWindowW, 100);
                    [self addMidViewRect:rect dataArr:model.attribute_values];
                    _midView.attributeModel = model;
                    _midView.frame = CGRectMake(0, 0, kWindowW, [_midView getViewHeight]);
                    [mStr appendFormat:@"%@,", model.product_attribute_name];
                }
                else
                {
                    CGRect rect = CGRectMake(0, CGRectGetMaxY(_midView.frame), kWindowW, 100);
                    [self addsubBtnViewRect:rect dataArr:model.attribute_values num:1];
                    _subView.attributeModel = model;
                    _subView.frame = CGRectMake(0, CGRectGetMaxY(_midView.frame), kWindowW, [_subView getViewHeight]);
                    [mStr appendFormat:@"%@", model.product_attribute_name];
                }
                
            }
            
            _selSizeLa.text = mStr;
            
            if (_midView.height + _subView.height > 225) {
                _btnSelView.frame = CGRectMake(0, CGRectGetMaxY(_topView.frame), kWindowW, 225);
                _btnSelView.contentSize = CGSizeMake(kWindowW, _midView.height + _subView.height);
            }
            else
            {
                _btnSelView.frame = CGRectMake(0, CGRectGetMaxY(_topView.frame), kWindowW, _midView.height + _subView.height);
            }
        }

    }
    else
    {
        if (_productAttributeBlock) {
            _productAttributeBlock(nil);
        }
        _btnSelView.frame = CGRectMake(0, _topView.height, kWindowW, 0);
        _selSizeLa.hidden = YES;
    }
    
}

- (void)addMidViewRect:(CGRect)rect dataArr:(NSArray *)arr
{
    _midView = [[JShopCarSelBtnView alloc]initWithFrame:rect dataArr:arr];
    [_btnSelView addSubview:_midView];
    
    __weak typeof(self) weakSelf = self;
    _midView.selBtnBlock = ^(NSInteger num) {
        [weakSelf BtnSelClick:num sel:0];
    };
    
}

- (void)addsubBtnViewRect:(CGRect)rect dataArr:(NSArray *)arr num:(NSInteger)numBer
{
    _subView = [[JShopCarSelBtnView alloc]initWithFrame:rect dataArr:arr];
    [_btnSelView addSubview:_subView];
    
    __weak typeof(self) weakSelf = self;

    _subView.selBtnBlock = ^(NSInteger num) {
        [weakSelf BtnSelClick:num sel:numBer];
    };
}

- (void)productBtnImgClick:(UIButton *)btn
{
    if (_imgBtnBlock) {
        _imgBtnBlock(_imgArr);
    }
}

- (void)cencleBtnClick:(UIButton *)btn
{
    if (_closeBtnBlock) {
        _closeBtnBlock();
    }
}

- (CGFloat)getViewHeight
{
    return 85+20 + _btnSelView.height;
}

@end
